﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VOD.Models;
using VOD.ViewModels;

namespace VOD.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class ServerMgrController : Controller
    {
        VODEntities db = new VODEntities();
        //
        // GET: /ServerMgr/
        public ActionResult Index()
        {
            //return View();
            return RedirectToAction("ServerInfo");
        }

        // 服务器信息
        // GET: /ServerMgr/ServerInfo
        public ActionResult ServerInfo()
        {
            //var info = new ServerInfo();
            //info.MachineName = Server.MachineName;
            //info.LocalAddr = Request.ServerVariables["LOCAL_ADDR"];
            //info.ServerName = Request.ServerVariables["SERVER_NAME"];
            //info.NET_CLR = String.Format(".NET CLR {0}.{1}.{2}.{3}", Environment.Version.Major, Environment.Version.Minor, Environment.Version.Build, Environment.Version.Revision);
            //info.OSVersion = Environment.OSVersion.ToString();
            //info.IISVersion = Request.ServerVariables["SERVER_SOFTWARE"];
            //info.Port = Request.ServerVariables["SERVER_PORT"];
            //info.AppPhysicalPath = Request.ServerVariables["APPL_RHYSICSL_PATH"];
            //info.PathTranslated = Server.MapPath("~");
            //info.SessionAmount = Session.Contents.Count.ToString();
            //info.HttpHost = Request.ServerVariables["HTTP_HOST"];
            //info.AcceptLanguage = Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
            //info.HttpUserAgent = Request.ServerVariables["HTTP_USER_AGENT"];
            //info.CPU_Amount = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");
            //info.CPU_Identifer = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");
            //return View(info);
            return View();
        }

        /// <summary>
        /// 网站访问统计
        /// </summary>
        /// <returns></returns>
        public ActionResult SiteCounter()
        {
            var counts = (from c in db.SiteCounts
                          group c by c.IP_Addr into g
                          select new SiteCounterChart()
                          {
                              IP_Addr = g.Key,
                              Counts = g.Count()
                          }).OrderByDescending(c => c.Counts);
            ViewBag.Count = db.SiteCounts.Count();
            return View(counts.Take(20));
        }

        public ActionResult SystemLog() 
        {
            var logs = db.SystemLogs.OrderByDescending(l => l.AddDate);
            return View(logs);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
